home *** CD-ROM | disk | FTP | other *** search
- Path: castle.nando.net!news
- From: actuary@nando.net (Bill McCarthy)
- Newsgroups: comp.lang.c
- Subject: Re: HELP!! with file read/write
- Date: 19 Mar 1996 00:30:47 GMT
- Organization: Nando.net Public Access
- Message-ID: <4ikv7n$392@castle.nando.net>
- References: <4iijmh$brs@cloner2.ix.netcom.com>
- Reply-To: actuary@nando.net (Bill McCarthy)
- NNTP-Posting-Host: grail407.nando.net
- X-Newsreader: IBM NewsReader/2 v1.2
-
- In <4iijmh$brs@cloner2.ix.netcom.com>, scoshe@ix.netcom.com(Christopher Scott Shelton ) writes:
- >
- >This part of my program should read a record, decrease daysleft by one,
- >and set today equal to zero and re-write the data at the original position.
- >For some reason it does one read, the following write and then it exits out.
- >I know that it should run 4 times. Can anyone help me with this problem.
- >
- >void daily_maintainence()
- >{
- > FILE *itemfile;
- > long filepos;
- > struct _items item;
- > itemfile=fopen("items.dat","r+b"); //open for read and write
- > while (fread(&item,itemsize,1,itemfile)==1)
- > {
- > filepos=ftell(itemfile); //for debug
- > item.today=0; //set to zero
- > item.daysleft--; //decrement
- > fseek(itemfile,-itemsize,SEEK_CUR); //seek to where this rec starts
- > filepos=ftell(itemfile); //for debug
- > fwrite(&item,itemsize,1,itemfile); //write it out
- > filepos=ftell(itemfile); //for debug
- > }
- >}
-
- This code will not compile. Your use of C++ style comments are syntax
- errors in C. Without an appropriate include file, C doesn't know about
- FILE. You have not defined itemsize. You have declared, but not
- defined item (so item.today and item.daysleft are meaningless).
-
- All this aside, an fread cannot follow an fwrite here without first
- performing a positioning function. Perhaps adding the following
- after fwrite() will help:
-
- fseek( itemfile, 0, SEEK_CUR);
-
- Bill McCarthy
- actuary@nando.net
- Wendell, NC USA
-
-